<?php

class Newsletter_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	// --------------------------------------------------------------------

	/**
	 * function SaveForm()
	 *
	 * insert form data
	 * @param $form_data - array
	 * @return Bool - TRUE or FALSE
	 */

	function SaveForm($form_data)
	{
		$this->db->insert('newsletter', $form_data);

		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}

		return FALSE;
	}
	function Delete($email)
	{
		$this->db->delete('newsletter', array('email' => $email));
		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}

		return FALSE;
	}
	function check_send($email,$sent=1)
	{
		$this->db->where('email', $email);
		$this->db->update('newsletter', array('sent' => $sent));
		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		$this->db->insert('newsletter', array('sent' => $sent,'email'=> $email));
		return FALSE;
	}
	public function get_newsletter_count(){
		$this->db->where('sent !=', 1);
		return $this->db->count_all_results('newsletter');
	}
	public function get_newsletters($limit, $offset){
		if($offset=='') return $this->db->get_where('newsletter',array('sent !=' => 1))->result();
		return $this->db->get_where('newsletter',array('sent !=' => 1),$limit, $offset)->result();
	}
	public function reset(){
		return $this->db->update('newsletter', array('sent' => 0));
	}
	public function insertEmail($email,$sent=0){
		$query=$this->db->get_where('newsletter',array('email' => $email),1)->row();
		if(!isset($query->email))
			$this->db->insert('newsletter', array('sent' => $sent,'email'=> $email));
	}
}